<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .input-box {
            margin: 10px;
            display: flex;
            align-items: center;
        }

        .input-box input {
            flex: 1;
            height: 40px;
            border: 1px solid #ccc;
            outline: none;
            padding-left: 20px;
            border-radius: 5px;
        }

        .input-box span {
            text-align: center;
            color: lightseagreen;
            width: 60px;
            cursor: pointer;
        }

        .list {
            margin: 10px;
        }

        .list .task {
            margin: 20px 0;
            display: flex;
            justify-content: space-between;
            padding: 15px;
            background-color: coral;
            color: white;
        }

        .list .task.finished {
            background-color: cornflowerblue;
        }
        .list .task span{
            cursor: pointer;
        }
        .empty {
            text-align: center;
            margin: 50px 0;
            color: red;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="input-box">
            <input type="text" placeholder="输入任务名称" v-model="inputText">
            <!-- v-on 的缩写形式 -->
            <span @click="add">添加</span>
        </div>
        <div class="list">
            <div v-bind:class="`task ${item.status?'finished':''}`" v-for="(item,index) in tasklist" :key="index">
                <input type="checkbox" @click="changeStatus(item)">
                <div class="name">{{item.name}}</div>
                <span class="btn" @click="deleteTask(item)">删除</span>
            </div>
            <!-- 没有内容时的提示文字，v-if/v-show，v-if内存消耗比较大 -->
            <div class="empty" v-show="tasklist.length == 0">任务列表为空!</div>
        </div>
    </div>
</body>

</html>
<script src="./vue-2.5.21.js"></script>
<script>
    var v = new Vue({
        el: '#app',
        data: {
            inputText: '',//保存输入框内的值
            tasklist: [

            ]
        },
        methods: {
            add() {//添加任务的方法
                //输入框有内容才添加任务
                if (this.inputText) {
                    //构造一个任务对象
                    var obj = { id: this.tasklist.length + 1, name: this.inputText, status: false };
                    //将任务对象添加到任务列表中
                    this.tasklist.push(obj);
                    //清空输入框
                    this.inputText = '';
                }
            },
            changeStatus(item) {
                item.status = !item.status;
            },
            deleteTask(item) {
                //查找到触发删除事件的元素
                var i = this.tasklist.findIndex((it) => {
                    return it.id == item.id
                    //找到返回元素下标，没找到返回-1
                })

                //删除元素
                if(i!=-1){
                    this.tasklist.splice(i,1)
                }
            }
        }
    })
</script>